<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      思路：层序遍历每一层，每一层的节点判断是否有子节点，如果没有子节点，就返回当前层级即可
      */
      var minDepth = function (root) {
        let level = 0
        function levelOrder(root) {
          if (!root) return []
          let queue = [root]
          let res = []
          while (queue.length) {
            let len = queue.length
            res.push([])
            level++
            for (let i = 0; i < len; i++) {
              let node = queue.shift()
              if (!node.left && !node.right) {
                return level
              }
              if (node.left) {
                queue.push(node.left)
              }
              if (node.right) {
                queue.push(node.right)
              }
            }
          }
        }
        levelOrder(root)
        return level
      }
    </script>
  </body>
</html>
